﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

using SimpleOrderClient.OrderServiceReference;

namespace SimpleOrderClient
{
    public partial class Form1 : Form
    {
        private OrderContractClient proxy = null;

        public Form1()
        {
            InitializeComponent();

            proxy = new OrderContractClient();
        }

        private void btnProduce_Click(object sender, EventArgs e)
        {
            try
            {
                proxy.ProduceItems(Decimal.Parse(ProduceItems.Text));
                MessageBox.Show("OK");
                ProduceItems.Clear();

                Balance_Click(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Produce Items failed", MessageBoxButtons.OK);
            }
        }

       
        private void Balance_Click(object sender, EventArgs e)
        {
            try
            {
                decimal balance = proxy.GetTotalQuantity();
                BalanceBox.Text = balance.ToString("F2");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Get Total Quantity exception", MessageBoxButtons.OK);
            }
        }

        private void btnConsume_Click(object sender, EventArgs e)
        {
            try
            {
                proxy.ConsumeItems(Decimal.Parse(ConsumeItems.Text));
                MessageBox.Show("OK");
                ConsumeItems.Clear();

                Balance_Click(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Produce Items failed", MessageBoxButtons.OK);
            }
        }

        private void ProduceItems_TextChanged(object sender, EventArgs e)
        {
            btnProduce.Enabled = ProduceItems.Text.Trim().Length > 0;
        }

        private void ConsumeItems_TextChanged(object sender, EventArgs e)
        {
            btnConsume.Enabled = ConsumeItems.Text.Trim().Length > 0;
        }

        private void ProduceItems_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                btnProduce_Click(sender, e);
            }
            else if (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9
               || e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
            {
                e.SuppressKeyPress = false;
            }
            else
            {
                e.SuppressKeyPress = true;
            }
        }

        private void ConsumeItems_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
               btnConsume_Click(sender, e);
            }
            else if (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9
               || e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
            {
                e.SuppressKeyPress = false;
            }
            else
            {
                e.SuppressKeyPress = true;
            }
        }

        
        
    }
}
